﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Medicalsystem.MCP.FormDesigner.Ctl
{
    public partial class ctl_ModelTree : DevExpress.XtraEditors.XtraUserControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="control"></param>
        public delegate void ClickHandler(ModelShowEventArgs e);

        /// <summary>
        /// 外部事件传递模板的选择消息
        /// </summary>
        public event ClickHandler ModelSelectedEvent;

        /// <summary>
        /// 按键的X坐标
        /// </summary>        
        private int Point_X;

        //执行comboBox_SubControl_SelectedIndexChanged事件时刷新了设计容器 
        //触发了MyPropertyGrid_Leave事件（此时不需要触发该事件） 用一个开关控制事件的执行
        private bool _leaveEventAvail = true;

        private int _width = 0; //控件的可用最大宽度
        private int _offset = 100; //收缩偏移量
        private bool _visible = false; //用于收缩控制

        Timer checkTimer = new Timer();    //定时器

        public ctl_ModelTree()
        {
            InitializeComponent();

            _width = this.Width;
            checkTimer.Interval = 50;
            checkTimer.Tick += new EventHandler(checkTimer_Tick);
            checkTimer.Stop();
        }

        /// <summary>
        /// 是否显示控件
        /// </summary>
        public bool VisibleCustom
        {
            get
            {
                return this.Visible;
            }
            set
            {
                _visible = value;

                //显示
                if (_visible && !this.Visible)
                {
                    this.Width = 1;
                    this.Visible = true;
                    this.checkTimer.Start();
                }
                else if (!_visible && this.Visible)
                {
                    _width = this.Width;
                    this.checkTimer.Start();
                }
            }
        }

        /// <summary>
        /// 添加模板
        /// </summary>
        /// <param name="modeID">编号</param>
        /// <param name="modeName">名称</param>
        /// <param name="modeDes">描述</param>
        public void Add(string modeID, string modeName, string modeDes)
        {
            this.treeView1.Nodes.Add(CreateTreeNode(modeID, modeName, modeDes));
        }

        public void Insert(int index, string modeID, string modeName, string modeDes)
        {
            this.treeView1.Nodes.Insert(index, CreateTreeNode(modeID, modeName, modeDes));
        }

        public void ShowFistModel()
        {
            if (this.treeView1.Nodes.Count > 0)
            {
                OnModelSelectedEvent(new ModelShowEventArgs(this.treeView1.Nodes[0].Name, this.treeView1.Nodes[0].Text));
            }
        }

        private TreeNode CreateTreeNode(string modeID, string modeName, string modeDes)
        {
            TreeNode TN = new TreeNode();
            TN.Name = modeID;
            TN.Text = modeName;
            TN.Tag = modeDes;
            TN.ToolTipText = "双击加载模板";

            return TN;
        }

        /// <summary>
        /// 模板树
        /// </summary>
        public TreeView TreeModel
        {
            get { return this.treeView1; }
        }

        /// <summary>
        /// 收缩控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkTimer_Tick(object sender, EventArgs e)
        {
            //显示
            if (_visible)
            {
                if (_width < (this.Width + _offset))
                {
                    this.Width = _width;
                    this.checkTimer.Stop();
                    this.Refresh();
                }
                else
                {
                    this.Width += _offset;
                }
            }
            else
            {
                if (this.Width - _offset <= 0)
                {
                    this.checkTimer.Stop();
                    this.Visible = false;
                }
                else
                {
                    this.Width -= _offset;
                }
            }
        }

        private void ctl_HeaderPin_AutoHide_Click(object sender, EventArgs e)
        {
            if (this.ctl_HeaderPin_AutoHide.AutoHide)
            {
                this.ctl_HeaderPin_AutoHide.AutoHide = false;
            }
            else
            {
                this.ctl_HeaderPin_AutoHide.AutoHide = true;
                this.Focus();
            }
        }

        private void ctl_HeaderClose1_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void pal_Left_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point_X = System.Windows.Forms.Control.MousePosition.X;
            }
        }

        private void pal_Left_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = System.Windows.Forms.Control.MousePosition;
                this.Width = this.Width - (mousePos.X - Point_X);
                Point_X = mousePos.X;
                this._width = this.Width;
                this.Refresh();
            }
        }

        private void ctl_ModelTree_Leave(object sender, EventArgs e)
        {
            if (this.ctl_HeaderPin_AutoHide.AutoHide && _leaveEventAvail)
            {
                this.VisibleCustom = false;
            }
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                _leaveEventAvail = false;

                OnModelSelectedEvent(new ModelShowEventArgs(e.Node.Name, e.Node.Text));

                _leaveEventAvail = true;
                this.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OnModelSelectedEvent(ModelShowEventArgs e)
        {
            if (ModelSelectedEvent != null)
            {
                ModelSelectedEvent(e);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                this.label_Name.Text = this.treeView1.SelectedNode.Text;
                this.label_Des.Text = this.treeView1.SelectedNode.Tag.ToString();
            }
        }
    }
}
